<template>
  <div>
    <div class="breadcrumbArea">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>模型管理</el-breadcrumb-item>
        <el-breadcrumb-item>新增</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-card class="box-card">
      <el-form
        :model="modelForm"
        :rules="rules"
        ref="modelForm"
        label-width="100px"
        class="demo-modelForm"
      >
        <el-form-item label="模型名称" prop="name">
          <el-input v-model="modelForm.name"></el-input>
        </el-form-item>

        <el-form-item label="模型类型" prop="modelType">
          <el-select v-model="modelForm.modelType" placeholder="请选择模型类型">
            <el-option
              v-for="item in modelTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="模型文件" prop="filePath">
          <el-upload
            class="upload-demo"
            drag
            :limit="1"
            :action="uploadUrl"
            :file-list="fileList"
            :on-success="handleFileSuccess"
            :on-error="handleFileError"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
            <div class="el-upload__tip" slot="tip">
              只能上传模型文件，且不超过100MB
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="备注" prop="comment">
          <el-input type="textarea" v-model="modelForm.comment"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('modelForm')"
            >新增</el-button
          >
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { addModel } from "@/api/model";
import { queryModelTypeList } from "@/api/modelType";

export default {
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/file/uploadModel",
      modelTypeList: [],
      modelForm: {
        name: "",
        modelType: "",
        filePath: "",
        comment: "",
      },
      rules: {
        name: [{ required: true, message: "请输入模型名称", trigger: "blur" }],
        modelType: [
          { required: true, message: "请选择模型类型", trigger: "change" },
        ],
        filePath: [
          { required: true, message: "请上传模型文件", trigger: "change" },
        ],
        comment: [
          { required: true, message: "请填写模型描述", trigger: "blur" },
        ],
      },
      fileList: [],
    };
  },
  created() {
    this.queryModelTypeList();
  },
  methods: {
    queryModelTypeList() {
      queryModelTypeList()
        .then((response) => {
          console.log(response);
          this.modelTypeList = response.data.records;
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    handleFileSuccess(response, file, fileList) {
      console.log(response);
      console.log(file);
      console.log(fileList);
      this.modelForm.filePath = response.filePath;
    },
    handleFileError(err, file, fileList) {
      console.log(err);
      console.log(file);
      console.log(fileList);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addModel(this.modelForm)
            .then((response) => {
              console.log(response);
              this.$message({
                type: "success",
                message: "新增成功!",
              });
              this.$router.go(-1);
            })
            .catch(function (e) {
              console.log(e);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>

<style scoped>
.breadcrumbArea {
  margin-bottom: 10px;
}
.box-card {
  width: 600px;
}
</style>
